package com.example.assignment01_javaee.Controller;

import com.example.assignment01_javaee.Entity.Product;
import com.example.assignment01_javaee.Service.ProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/api")
public class ProductController {
    @Autowired
    private ProductService productService;

    @GetMapping("/products")
    public ResponseEntity<List<Product>> getAllProducts() {
        List<Product> products = productService.getAllProducts();
        return ResponseEntity.ok(products);
    }

    @GetMapping("/products/{productId}")
    public ResponseEntity<Product> getProductById(@PathVariable int productId) {
        Product product = productService.getProductById(productId);
        return ResponseEntity.ok(product);
    }

    @PostMapping("/products")
    public String createProduct(@RequestBody Product product) {
        productService.createProduct(product);
        return "增加商品成功";
    }

    @PutMapping("/products/{productId}")
    public String updateProduct(@PathVariable int productId, @RequestBody Product product) {
        product.setProductId(productId);
        productService.updateProduct(product);
        return "修改商品成功";
    }

    @DeleteMapping("/products/{productId}")
    public String deleteProduct(@PathVariable int productId) {
        productService.deleteProduct(productId);
        return "删除商品成功";
    }
}
